<%--
  Created by IntelliJ IDEA.
  User: lujw
  Date: 2017/6/14
  Time: 18:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/common/validate.jsp"%>
<%@ include file="/common/hospital_notice.jsp"%>
<%!
    /**
     * 查看阅读列表(分页)
     * @param input
     * @return
     */
    public Message readList(Input input) {
        Integer page = 1; // 页码
        Integer itemCount = 20; // 每一页条目
        Integer hospital = input.getInt("hospital", -1);
        //通知id
        Integer noticeId = input.getInt("notice_id", -1);
        //通知阅读id
        Integer readId = input.getInt("read_id", -1);
        //已读1，未读2
        Integer type = input.getInt("type", 1);
        //refresh下拉刷新，load上拉加载
        String act = input.getDesc("act", "refresh");
        String selectContent = "new map(u.id as user_id, u.name as name, f.readTime as time, u.phone as phone, u.avatar as avatar, u.gender as gender, f.id as id)";
        QBuilder qBuilder = QBuilder.db("basedb").select(selectContent).from("HospitalUser u, HospitalNoticeFeedback f");
        Where where = new Where("u.status=", ConstantUtil.STATUS_ENABLE).and("f.status=", ConstantUtil.STATUS_ENABLE).and("f.noticeId=", noticeId).and("u.id=f.userId").and("u.hospital=", hospital).and("f.hospital=", hospital);
        if (type == 1) {
            //已读列表
            //阅读时间
            String readTime = input.getDesc("read_time", "");
            String order = "";
            if ("refresh".equals(act)) {
                //下拉刷新
                order = "asc";
                if (!"".equals(readTime) && readId != -1)
                    where.and("f.readTime > '"+ readTime + "' or (f.readTime = '" + readTime + "' and f.id > " + readId + ")");
            } else {
                //上拉加载
                order = "desc";
                if (!"".equals(readTime) && readId != -1)
                    where.and("f.readTime < '"+ readTime + "' or (f.readTime = '" + readTime + "' and f.id < " + readId + ")");
            }
            where.and("f.readTime is not null");
            qBuilder.where(where).order("f.readTime desc,f.id ", order);
        } else {
            //未读列表
            //上拉加载
            if (readId != -1) {
                where.and("f.id < " + readId);
            }
            where.and("f.readTime is null");
            qBuilder.where(where).order("f.id", QBuilder.desc);
        }
        List<Map> list = qBuilder.page(itemCount, page).query().getResultObj();
        if (type == 1 && "refresh".equals(act)) {
            Collections.sort(list, new Comparator<Map>() {
                @Override
                public int compare(Map o1, Map o2) {
                    Date firstDate = DateUtil.parseDateTime(Tools.getDesc(o1.get("time")));
                    Date lastDate = DateUtil.parseDateTime(Tools.getDesc(o2.get("time")));
                    Integer dateCompareResult = Objects.isNull(lastDate) ? -1 : Objects.isNull(firstDate) ? 1 : lastDate.compareTo(firstDate);
                    if (dateCompareResult == 0) {
                        Integer firstId = (Integer)o1.get("id");
                        Integer lastId = (Integer)o2.get("id");
                        return Integer.compare(lastId, firstId);
                    }
                    return dateCompareResult;
                }
            });
        }
        completeUserDept(list, hospital);
        Message msg = new Message();
        msg.put("data", list);
        return msg;
    }
%>
<%
    Tools.encryptPrint(out, readList(input));
%>
